package org.chen.gateway.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class R<T> {
    private Integer code;
    private String msg;
    private T data;

    // 成功响应
    public static <T> R<T> success() {
        return R.<T>builder()
                .code(200)
                .msg("操作成功")
                .data(null)
                .build();
    }

    public static <T> R<T> success(T data) {
        return R.<T>builder()
                .code(200)
                .msg("操作成功")
                .data(data)
                .build();
    }

    public static <T> R<T> success(String msg, T data) {
        return R.<T>builder()
                .code(200)
                .msg(msg)
                .data(data)
                .build();
    }

    // 失败响应
    public static <T> R<T> Failed() {
        return R.<T>builder()
                .code(500)
                .msg("操作失败")
                .data(null)
                .build();
    }

    public static <T> R<T> Failed(String msg) {
        return R.<T>builder()
                .code(500)
                .msg(msg)
                .data(null)
                .build();
    }

    public static <T> R<T> Failed(Integer code, String msg) {
        return R.<T>builder()
                .code(code)
                .msg(msg)
                .data(null)
                .build();
    }

    // 未授权响应
    public static <T> R<T> unauthorized() {
        return R.<T>builder()
                .code(401)
                .msg("未授权")
                .data(null)
                .build();
    }

    public static <T> R<T> unauthorized(String msg) {
        return R.<T>builder()
                .code(401)
                .msg(msg)
                .data(null)
                .build();
    }
}